using UnityEngine;

public class lerpTransformPhoton : PhotonTransformView
{
	private const int MAX_INCREMENT = 20;

	private Mortal _mortal;

	private TankTower tower;

	private RCC_CarControllerV3 _controllerScript;

	private Rigidbody rigid;

	private RCC_Camera cameraScript;

	private Vector3 correctPlayerPos;

	private Quaternion correctPlayerRot;

	private Vector3 currentVelocity;

	private float updateTime;

	private float gasInput;

	private float brakeInput;

	private float steerInput;

	private float handbrakeInput;

	private float boostInput;

	private float clutchInput;

	private float idleInput;

	private int gear;

	private int direction = 1;

	private bool changingGear;

	public bool semiAutomaticGear;

	public float fuelInput = 1f;

	public bool engineRunning;

	private int lastSetedHealth = 1000000;

	public bool onBoost;

	[HideInInspector]
	public int incrementHealth;

	private int lastSetedIncrementHealth;

	private RCC_CarControllerV3 controllerScript
	{
		get
		{
			if (_controllerScript == null)
			{
				_controllerScript = GetComponentInChildren<RCC_CarControllerV3>(true);
				rigid = GetComponent<Rigidbody>();
			}
			return _controllerScript;
		}
	}

	private Mortal mortal
	{
		get
		{
			if (_mortal == null)
			{
				_mortal = GetComponentInChildren<Mortal>(true);
			}
			return _mortal;
		}
	}

	private void Start()
	{
		Invoke("FindTower", 2f);
		if (settings.offlineMode)
		{
			base.enabled = false;
		}
	}

	private void FindTower()
	{
		tower = GetComponentInChildren<TankTower>(true);
	}

	private void PuckSteamToRace(PhotonStream stream)
	{
		if (controllerScript == null)
		{
			return;
		}
		if (stream.isWriting)
		{
			stream.SendNext(controllerScript.gasInput);
			stream.SendNext(controllerScript.brakeInput);
			stream.SendNext(controllerScript.steerInput);
			stream.SendNext(controllerScript.handbrakeInput);
			stream.SendNext(controllerScript.boostInput);
			stream.SendNext(controllerScript.clutchInput);
			stream.SendNext(controllerScript.idleInput);
			stream.SendNext(controllerScript.currentGear);
			stream.SendNext(controllerScript.direction);
			stream.SendNext(controllerScript.changingGear);
			stream.SendNext(controllerScript.engineRunning);
			stream.SendNext(base.transform.position);
			stream.SendNext(base.transform.rotation);
			stream.SendNext(rigid.velocity);
			stream.SendNext(controllerScript.entity.onBoost);
			if (controllerScript.carBehaiver.carWithWeapon)
			{
				stream.SendNext(controllerScript.carBehaiver.countBulletCar);
			}
			if (GameController.instance.curTypeGame == TypeGame.race)
			{
				stream.SendNext((mortal as CarBehavior).IsBlinking);
			}
			return;
		}
		gasInput = (float)stream.ReceiveNext();
		brakeInput = (float)stream.ReceiveNext();
		steerInput = (float)stream.ReceiveNext();
		handbrakeInput = (float)stream.ReceiveNext();
		boostInput = (float)stream.ReceiveNext();
		clutchInput = (float)stream.ReceiveNext();
		idleInput = (float)stream.ReceiveNext();
		gear = (int)stream.ReceiveNext();
		direction = (int)stream.ReceiveNext();
		changingGear = (bool)stream.ReceiveNext();
		engineRunning = (bool)stream.ReceiveNext();
		correctPlayerPos = (Vector3)stream.ReceiveNext();
		correctPlayerRot = (Quaternion)stream.ReceiveNext();
		currentVelocity = (Vector3)stream.ReceiveNext();
		onBoost = (bool)stream.ReceiveNext();
		updateTime = Time.unscaledTime;
		if (controllerScript.carBehaiver.carWithWeapon)
		{
			controllerScript.carBehaiver.setCountBullets((int)stream.ReceiveNext());
		}
		if (GameController.instance.curTypeGame == TypeGame.race)
		{
			bool flag = (bool)stream.ReceiveNext();
			CarBehavior carBehavior = mortal as CarBehavior;
			if (carBehavior.IsBlinking && !flag)
			{
				carBehavior.StopBlinking();
			}
			if (!carBehavior.IsBlinking && flag)
			{
				carBehavior.StartBlinking();
			}
		}
	}

	public override void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
	{
		if ((GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.RocketLeague) && controllerScript != null)
		{
			PuckSteamToRace(stream);
		}
		else
		{
			base.OnPhotonSerializeView(stream, info);
		}
		if (stream.isWriting)
		{
			if (mortal != null)
			{
				stream.SendNext(mortal.health);
				stream.SendNext(mortal.health > lastSetedHealth);
				if (incrementHealth > 20)
				{
					incrementHealth = 0;
				}
				stream.SendNext(incrementHealth);
				lastSetedHealth = mortal.health;
			}
			if (tower != null)
			{
				stream.SendNext(tower.transform.localRotation);
				if ((bool)tower.cannon)
				{
					stream.SendNext(tower.cannon.transform.localRotation);
				}
			}
			return;
		}
		if (mortal != null)
		{
			int num = (int)stream.ReceiveNext();
			bool flag = (bool)stream.ReceiveNext();
			int num2 = (int)stream.ReceiveNext();
			if (num2 != lastSetedIncrementHealth || (lastSetedHealth != num && (num < mortal.health || flag)))
			{
				if (num2 != lastSetedIncrementHealth)
				{
					mortal.maxHealth = num;
				}
				mortal.health = num;
				if (mortal.health <= 0)
				{
					mortal.Kill();
				}
				lastSetedHealth = num;
			}
			lastSetedIncrementHealth = num2;
		}
		if (tower != null && stream.Count > 0)
		{
			tower.newTowerRotation = (Quaternion)stream.ReceiveNext();
			if ((bool)tower.cannon)
			{
				tower.newCannonRotation = (Quaternion)stream.ReceiveNext();
			}
		}
	}

	public void FixedUpdate()
	{
		if (GameController.instance == null || mortal == null || (GameController.instance.curTypeGame != TypeGame.race && GameController.instance.curTypeGame != TypeGame.RocketLeague))
		{
			return;
		}
		if (!engineRunning)
		{
			if (!mortal.isMine)
			{
				base.transform.position = correctPlayerPos;
				base.transform.rotation = correctPlayerRot;
			}
		}
		else
		{
			if (!(controllerScript != null) || mortal.isMine)
			{
				return;
			}
			CarBehavior carBehavior = mortal as CarBehavior;
			if ((GameController.instance.curTypeGame == TypeGame.race || GameController.instance.curTypeGame == TypeGame.RocketLeague) && updateTime > 0f)
			{
				if (carBehavior.IsBlinking && Time.unscaledTime - updateTime < 1.25f)
				{
					carBehavior.StopBlinking();
				}
				if (!carBehavior.IsBlinking && Time.unscaledTime - updateTime > 1.25f)
				{
					carBehavior.StartBlinking();
					ResetParams();
				}
			}
			float num = 25f;
			if (GameController.instance.curTypeGame == TypeGame.RocketLeague)
			{
				num = 75f;
			}
			float num2 = Vector3.Distance(base.transform.position, correctPlayerPos);
			if (onBoost || num2 < num)
			{
				if (num2 > 0.5f)
				{
					Vector3 b = correctPlayerPos + currentVelocity * (Time.unscaledTime - updateTime + (float)(PhotonNetwork.GetPing() / 1000));
					base.transform.position = Vector3.Lerp(base.transform.position, b, Time.unscaledDeltaTime * 5f);
					base.transform.rotation = Quaternion.Lerp(base.transform.rotation, correctPlayerRot, Time.unscaledDeltaTime * 5f);
				}
			}
			else
			{
				ResetParams();
				base.transform.position = correctPlayerPos;
				base.transform.rotation = correctPlayerRot;
			}
			rigid.isKinematic = !engineRunning || carBehavior.IsBlinking;
			rigid.velocity = Vector3.Lerp(rigid.velocity, currentVelocity, Time.unscaledDeltaTime * 4f);
			if (GameController.instance.curTypeGame != TypeGame.tanks)
			{
				controllerScript.gasInput = gasInput;
				controllerScript.brakeInput = brakeInput;
				controllerScript.steerInput = steerInput;
				controllerScript.handbrakeInput = handbrakeInput;
				controllerScript.boostInput = boostInput;
				controllerScript.clutchInput = clutchInput;
				controllerScript.idleInput = idleInput;
				controllerScript.currentGear = gear;
				controllerScript.direction = direction;
				controllerScript.changingGear = changingGear;
				controllerScript.engineRunning = engineRunning;
				controllerScript.enabled = engineRunning;
				controllerScript.entity.onBoost = onBoost;
			}
			else
			{
				controllerScript.enabled = false;
			}
		}
	}

	private void ResetParams()
	{
		gasInput = 0f;
		brakeInput = 1f;
		steerInput = 0f;
		gear = 0;
		direction = 0;
		engineRunning = false;
		currentVelocity = Vector3.zero;
		onBoost = false;
	}
}
